package club.ruanx.movie.util;

import club.ruanx.acl.entity.SysParam;
import club.ruanx.acl.service.SysParamService;
import club.ruanx.movie.common.constraint.MovieParamConstraint;
import club.ruanx.movie.entity.Movie;
import club.ruanx.search.feign.client.SolrMovieServiceClient;
import club.ruanx.solr.param.SolrMovieParam;
import club.ruanx.util.BeanUtils;
import club.ruanx.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 阮胜
 * @date 2019/2/15 19:24
 */
@Component
@Slf4j
public class SolrUpdater {
    private final SolrMovieServiceClient solrMovieServiceClient;
    private final SysParamService sysParamService;

    public SolrUpdater(SolrMovieServiceClient solrMovieServiceClient, SysParamService sysParamService) {
        this.solrMovieServiceClient = solrMovieServiceClient;
        this.sysParamService = sysParamService;
    }

    public void updateToSolrServer(Movie movie) {
        log.info("向搜索引擎服务器更新电影:{}", movie.getName());
        try {
            SolrMovieParam solrMovieParam = adaptSolrMovie(movie);
            solrMovieServiceClient.save(solrMovieParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private SolrMovieParam adaptSolrMovie(Movie movie) {
        SolrMovieParam solrMovieParam = new SolrMovieParam();
        BeanUtils.copyPropertiesIgnoreNull(movie, solrMovieParam);
        List<String> types = JSON.parseArray(movie.getTypes(), String.class);
        List<SysParam> typesParamList = sysParamService.findAllByFlag(MovieParamConstraint.MOVIE_TYPE);
        if (!CollectionUtils.isEmpty(types)) {
            String[] typeArr = types.stream()
                    .map(type -> typesParamList.stream()
                            .filter(sysParam -> sysParam.getValue().equals(type))
                            .map(SysParam::getName)
                            .findFirst().orElse(null))
                    .toArray(value -> new String[types.size()]);
            solrMovieParam.setTypes(JSON.toJSONString(typeArr));
        }
        List<String> languages = JSON.parseArray(movie.getLanguages(), String.class);
        List<SysParam> languageParamList = sysParamService.findAllByFlag(MovieParamConstraint.MOVIE_LANGUAGE);
        if (!CollectionUtils.isEmpty(languages)) {
            String[] languageArr = languages.stream()
                    .map(language -> languageParamList.stream()
                            .filter(sysParam -> sysParam.getValue().equals(language))
                            .map(SysParam::getName)
                            .findFirst().orElse(null))
                    .toArray(value -> new String[languages.size()]);
            solrMovieParam.setLanguages(JSON.toJSONString(languageArr));
        }
        List<String> areas = JSON.parseArray(movie.getAreas(), String.class);
        List<SysParam> areaParamList = sysParamService.findAllByFlag(MovieParamConstraint.MOVIE_AREA);
        if (!CollectionUtils.isEmpty(areas)) {
            String[] areasArr = areas.stream()
                    .map(area -> areaParamList.stream()
                            .filter(sysParam -> sysParam.getValue().equals(area))
                            .map(SysParam::getName)
                            .findFirst().orElse(null))
                    .toArray(value -> new String[areas.size()]);
            solrMovieParam.setAreas(JSON.toJSONString(areasArr));
        }
        return solrMovieParam;
    }
}
